package com.slh.touchandroid.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.slh.touchandroid.R;
import com.slh.touchandroid.fragment.BrowserFragment;
import com.slh.touchandroid.utils.Utils;

public class BrowserActivity extends BaseActivity implements
		OnItemClickListener {
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browser);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, 0, 0);
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		
		loadDrawer();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.browser, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	private void loadDrawer() {
		ArrayList<DrawerItem> items = new ArrayList<BrowserActivity.DrawerItem>();
		if (Utils.Memory.isExternalMemoryAvailable()) {
			items.add(new DrawerItem("External Storage",
					R.drawable.ic_folder_sdcard, Environment
							.getExternalStorageDirectory().getAbsolutePath()));
		}
		items.add(new DrawerItem("Device", R.drawable.ic_folder_device, "/"));
		items.add(new DrawerItem("Download", R.drawable.ic_folder_download,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()));
		items.add(new DrawerItem("DCIM", R.drawable.ic_folder_picture,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_DCIM).getAbsolutePath()));
		items.add(new DrawerItem("Pictures", R.drawable.ic_folder_picture,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_PICTURES).getAbsolutePath()));
		items.add(new DrawerItem("Music", R.drawable.ic_folder_music,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_MUSIC).getAbsolutePath()));
		items.add(new DrawerItem("Movies", R.drawable.ic_folder_movie,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_MOVIES).getAbsolutePath()));
		items.add(new DrawerItem("Ringtones", R.drawable.ic_folder_music,
				Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_RINGTONES).getAbsolutePath()));
		ListView lvDrawer = (ListView) findViewById(R.id.left_drawer);
		lvDrawer.setAdapter(new DrawerListAdapter(this, items));
		lvDrawer.setOnItemClickListener(this);
		getSupportFragmentManager()
				.beginTransaction()
				.replace(
						R.id.content_frame,
						BrowserFragment.newInstance(items.get(0).path,
								items.get(0).name, true)).commit();
	}

	private static class Holder {
		TextView tvName;
	}

	private class DrawerItem {
		public String name;
		public int resId;
		public String path;

		public DrawerItem(String name, int resId, String path) {
			super();
			this.name = name;
			this.resId = resId;
			this.path = path;
		}

	}

	private class DrawerListAdapter extends BaseAdapter {
		private Context context;
		private ArrayList<DrawerItem> items;

		public DrawerListAdapter(Context context, ArrayList<DrawerItem> items) {
			super();
			this.context = context;
			this.items = items;
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.row_common, null);
				holder = new Holder();
				holder.tvName = (TextView) convertView
						.findViewWithTag(getString(R.string.tag_name));
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			DrawerItem item = items.get(position);
			holder.tvName.setText(item.name);
			holder.tvName.setCompoundDrawablesWithIntrinsicBounds(item.resId,
					0, 0, 0);
			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position);
		getSupportFragmentManager()
				.beginTransaction()
				.replace(
						R.id.content_frame,
						BrowserFragment.newInstance(item.path, item.name, true),
						"").commit();
		mDrawerLayout.closeDrawers();

	}

}
